/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "overrideinstructionlogic.h"
#include "GeneralDefine.h"
#include "programengine.h"
#include "messagelog.h"


OverrideInstructionLogic::OverrideInstructionLogic(int robotIdIn, ProgramSet* programSetIn,
                                                   RobotIo* ioControllerIn,
                                                   ProgramEngine* programEngineIn,
                                                   McControllerThread *programMcControllerIn)
{
    programSet=programSetIn;
    robotId=robotIdIn;
    ioController=ioControllerIn;
    programEngine=programEngineIn;
    programMcController=programMcControllerIn;
}


E_PROGRAM_LOOP_RESULT OverrideInstructionLogic::startOverride(int programNum,
                                                              OverrideInstruction overrideInstruct)
{
    programEngine->updateProgramSetRegister();

    float autoSpeed;

    switch(overrideInstruct.overrideType)
    {
        case OVERRIDE_VALUE_AR:
        {

        }
        break;
        case OVERRIDE_VALUE_R:
        {
            if(D_ROBOT_REG_SIZE > overrideInstruct.overrideValue || 1 > overrideInstruct.overrideValue)
            {
                autoSpeed = programSet->regRList[overrideInstruct.overrideValue - 1];
            }
            else
            {
                programEngine->cmdError(programNum);
                addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  3017, robotId );
            }
        }
        break;
        case OVERRIDE_VALUE_CONST:
        {
            autoSpeed = overrideInstruct.overrideValue;
        }
        break;
    }

    programMcController->mcControllerInterpreter->setAutoSpeed(autoSpeed);

    programEngine->calculateNextLineToRun(programNum);
    E_PROGRAM_LOOP_RESULT endFlag = programEngine->isTheLastInstruction(programNum);
    return endFlag;
}


void OverrideInstructionLogic::addMsg(int messageLevel, string componentName, string messageType,
                           int messageCode, int robotId,
                           int parameter1, int parameter2, int parameter3, int parameter4,QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;


    QString infomationStr;

    switch(messageCode)
    {

    case 3017:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr + QObject::tr(",寄存器指令错误，寄存器的范围超过限定的最大范围，请修改");
    }
        break;



    default:
    {

    }
    }


    tmpMsg.MessageInformation = infomationStr.toStdString();

    // qDebug()<<"53444444444444444444444"<< tmpMsg.messageCode;
    MessageLog::getInstance()->addMessage(tmpMsg);
}
